template <class ForwardIterator, class UnaryPredicate> ForwardIterator partition_point (ForwardIterator first, ForwardIterator last, UnaryPredicate pred);
[first,last) for which pred is not true, indicating its partition point.1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
template <class ForwardIterator, class UnaryPredicate>
ForwardIterator partition_point (ForwardIterator first, ForwardIterator last,
UnaryPredicate pred)
{
auto n = distance(first,last);
while (n>0)
{
ForwardIterator it = first;
auto step = n/2;
std::advance (it,step);
if (pred(*it)) { first=++it; n-=step+1; }
else n=step;
}
return first;
}
[first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.bool. The value returned indicates whether the element goes before the partition point (if true, it goes before; if false goes at or after it).[first,last) for which pred is not true, or last if it is not true for any element.1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// partition_point example
#include <iostream> // std::cout
#include <algorithm> // std::partition, std::partition_point
#include <vector> // std::vector
bool IsOdd (int i) { return (i%2)==1; }
int main () {
std::vector<int> foo {1,2,3,4,5,6,7,8,9};
std::vector<int> odd;
std::partition (foo.begin(),foo.end(),IsOdd);
auto it = std::partition_point(foo.begin(),foo.end(),IsOdd);
odd.assign (foo.begin(),it);
// print contents of odd:
std::cout << "odd:";
for (int& x:odd) std::cout << ' ' << x;
std::cout << '\n';
return 0;
}
odd: 1 3 5 7 9
log2(N)+2 element comparisons (where N is this distance).[first,last) are accessed.